<template>
	<el-dialog
	  :title="onlyGet ? '只领取' : isUpdate && !onlyPlan ? '修改' : '入库信息'"
	  :visible.sync="visible"
	  :before-close="close"
    :append-to-body="true"
	  :close-on-click-modal="false"
	  width="50%">
    <el-collapse v-model="activeNames" v-for="(p,index) in projects" :key="index">
      <el-collapse-item :title="isUpdate ? p.planName : p.label" :name="index">
        <el-form :model="p" label-width="150px">
          <el-form-item>
            <md-input style="margin-left: 15%;margin-bottom: 50px;" v-model="p.planName" icon="el-icon-search" placeholder="输入计划名称">
              计划名称
            </md-input>
          </el-form-item>

          <el-form-item label="生产车间:">
            <el-select v-model="p.workshop" value-key="workshopId" @change="setWorkshop(index)" style="width: 100%;" filterable placeholder="生产车间" size="small">
              <el-option
                v-for="item in workshopList"
                :key="item.workshopId"
                :label="item.workshopName"
                :value="item">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="生产日期:">
            <el-date-picker
              v-model="p.productionDate"
              align="right"
              type="date"
              placeholder="选择日期"
              style="width: 100%;"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd HH:mm:ss"
              size="small">
            </el-date-picker>
          </el-form-item>

          <el-form-item label="有效期至:">
            <el-date-picker
              v-model="p.validityDate"
              align="right"
              type="date"
              placeholder="选择日期"
              style="width: 100%;"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd HH:mm:ss"
              size="small">
            </el-date-picker>
          </el-form-item>

          <el-form-item label="物料编码:">
            {{ p.productGmp }}
          </el-form-item>

          <el-form-item label="货品名称:">
            {{ p.productName }}
          </el-form-item>

          <el-form-item label="货品规格:">
            <span v-html="p.productSpec"></span>
          </el-form-item>

          <el-form-item label="包装规格:">
            {{ p.productPackSpec }}
          </el-form-item>

          <el-form-item label="批号:">
            {{ p.batchNum }}
          </el-form-item>

          <el-form-item label="入库数量(小单位):">
            <el-input :disabled="isUpdate" @change="setIncomingMini(index)" v-model="p.incomingMini" placeholder="数值" size="small">
              <el-select :disabled="isUpdate" @change="setIncomingMiniUnit(index)" value-key="unitId" v-model="p.incomingMiniUnit" slot="append" placeholder="单位" style="width: 100px;">
                <el-option v-for="item in unitList" :key="item.unitId" :label="item.unitName" :value="item" />
              </el-select>
            </el-input>
          </el-form-item>

          <el-form-item label="库存量:">
            {{ p.inventory + p.inventoryUnitName }}
          </el-form-item>

          <el-form-item label="入库数量(大单位):">
            <el-input :disabled="isUpdate" @change="setIncomingBig(index)" v-model="p.incomingBig" placeholder="数值" size="small">
              <el-select :disabled="isUpdate" @change="setIncomingBigUnit(index)" value-key="unitId" v-model="p.incomingBigUnit" slot="append" placeholder="单位" style="width: 100px;">
                <el-option v-for="item in unitList" :key="item.unitId" :label="item.unitName" :value="item" />
              </el-select>
            </el-input>
          </el-form-item>

          <el-form-item label="贮藏:" v-if="!onlyGet">
            <el-input v-model="p.storeEnv" size="small" />
          </el-form-item>

          <el-form-item label="温度:" v-if="!onlyGet">
            <el-input v-model="p.envT" size="small" />
          </el-form-item>

          <el-form-item label="湿度:" v-if="!onlyGet">
            <el-input v-model="p.envH" size="small" />
          </el-form-item>

          <el-form-item label="检测周期:" v-if="!onlyGet">
             <el-input @change="setCheckPeriodValue(index)" v-model="p.checkPeriodValue" placeholder="数值" size="small">
               <el-select @change="setCheckPeriodTime(index)" value-key="timeId" v-model="p.checkPeriodTime" slot="append" placeholder="单位" style="width: 100px;">
                 <el-option v-for="t in timeList" :key="t.timeId" :label="t.timeName" :value="t" />
               </el-select>
             </el-input>
          </el-form-item>

          <el-form-item label="检测频率:" v-if="!onlyGet">
            <el-input v-model="p.checkFrequency" size="small" />
          </el-form-item>

          <el-form-item label="包装:" v-if="!onlyGet">
            <el-input v-model="p.pack" size="small" />
          </el-form-item>

          <el-form-item label="存放日期:" :disabled="isUpdate">
            <el-date-picker
              @change="setTimePoint(index)"
              :id="'storeDate'+index"
              v-model="p.storeDate"
              align="right"
              type="date"
              placeholder="选择日期"
              style="width: 100%;"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd HH:mm:ss"
              size="small">
            </el-date-picker>
          </el-form-item>

          <el-form-item label="稳考总次数:" v-if="!onlyGet">
            {{ p.total + ' 次 ' }}
            <el-button type="text" @click="showProjectTime(index)">(配置样品取样计划)</el-button>
            <el-dialog
              :visible.sync="p.timeVisible"
              :append-to-body="true"
              width="95%">
              <el-table :data="p.times">
                <el-table-column>
                  <el-table-column width="100" label="快捷选项" header-align="center" align="center">
                    <template slot-scope="scope">
                      <el-button type="warning" class="pan-btn warning-btn" icon="el-icon-plus" @click="addTime(index,scope.$index)" circle size="mini" />
                      <el-button type="danger" class="pan-btn danger-btn" icon="el-icon-minus" :disabled="isUpdate && scope.row.status == 2" @click="subTime(index,scope.$index)" circle size="mini" />
                    </template>
                  </el-table-column>
                </el-table-column>
                <el-table-column label="状态" width="80" header-align="center" align="center">
                  <template slot-scope="scope">
                    <el-tag :type="scope.row.status == 2 ? 'success' : 'danger'">
                      {{ scope.row.status == 2 ? '已完成' : '未完成' }}
                    </el-tag>
                    <el-tag type="primary" v-if="p.status != 2 && scope.row.unlock" @click="changeLock(scope.$index)" class="point">
                      解锁
                    </el-tag>
                    <el-tag type="primary" v-if="p.status != 2 && !scope.row.unlock" @click="changeLock(scope.$index)" class="point">
                      上锁
                    </el-tag>
                  </template>
                </el-table-column>
                <el-table-column label="取样点" header-align="center" align="center">
                  <el-table-column width="150">
                    <template slot-scope="scope">
                      <el-input @change="setRowTimeValue(index,scope.$index)" :disabled="isUpdate && scope.row.status == 2" v-model="scope.row.timeValue" placeholder="数值" size="small">
                        <el-select @change="setRowTime(index)" value-key="timeId" v-model="p.rowTime" slot="append" placeholder="单位" style="width: 60px;">
                          <el-option v-for="t in timeList" :key="t.timeId" :label="t.timeName" :value="t" />
                        </el-select>
                      </el-input>
                    </template>
                  </el-table-column>
                </el-table-column>
                <el-table-column header-align="center" align="center">
                  <template slot="header" slot-scope="scope">
                    <div>取样量</div>
                    <div>总入库量：{{ p.incomingMini + p.incomingMiniUnitName }}</div>
                    <div :style="{color: p.surplus >= 0 ? '#409EFF' : '#F56C6C'}">剩余量：{{ p.surplus + p.incomingMiniUnitName }}</div>
                  </template>
                  <el-table-column width="150" header-align="center" align="center">
                    <template slot="header" slot-scope="scope">
                      <el-input @change="setAllGetQuantity(index)" v-model="p.allGetQuantity" placeholder="数值" size="small">
                        <el-select :disabled="isUpdate" @change="setIncomingMiniUnit(index)" value-key="unitId" v-model="p.incomingMiniUnit" slot="append" placeholder="单位" style="width: 60px;">
                          <el-option v-for="item in unitList" :key="item.unitId" :label="item.unitName" :value="item" />
                        </el-select>
                      </el-input>
                    </template>
                    <template slot-scope="scope">
                      <el-input :disabled="isUpdate && scope.row.status == 2" @change="setRowGetQuantity(index,scope.$index)" v-model="scope.row.getQuantity" placeholder="数值" size="small">
                        <el-select :disabled="isUpdate" @change="setIncomingMiniUnit(index)" value-key="unitId" v-model="p.incomingMiniUnit" slot="append" placeholder="单位" style="width: 60px;">
                          <el-option v-for="item in unitList" :key="item.unitId" :label="item.unitName" :value="item" />
                        </el-select>
                      </el-input>
                    </template>
                  </el-table-column>
                </el-table-column>
                <el-table-column label="具体取样时间" align="center" header-align="center">
                  <el-table-column width="150">
                    <template slot-scope="scope">
                      <el-date-picker
                        :disabled="isUpdate && scope.row.status == 2"
                        v-model="scope.row.timePoint"
                        @change="flushTimeArr(index,scope.$index)"
                        align="right"
                        type="date"
                        placeholder="选择日期"
                        style="width: 100%;"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        size="small">
                      </el-date-picker>
                    </template>
                  </el-table-column>
                </el-table-column>
                <el-table-column label="取样时间段" header-align="center" align="center">
                  <el-table-column width="230">
                    <template slot="header" slot-scope="scope">
                      <el-input v-model="p.nInterval" @change="setNInterval(index)" size="small">
                        <template slot="prepend">负</template>
                        <template slot="append">天</template>
                      </el-input>
                      <el-input style="margin-top: 5px;" v-model="p.pInterval" @change="setPInterval(index)" size="small">
                        <template slot="prepend">正</template>
                        <template slot="append">天</template>
                      </el-input>
                    </template>
                    <template slot-scope="scope">
                      <el-date-picker
                        :disabled="isUpdate && scope.row.status == 2"
                        v-model="scope.row.timeArr"
                        type="daterange"
                        range-separator="~"
                        start-placeholder="开始"
                        end-placeholder="结束"
                        style="width: 100%;"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        size="small">
                      </el-date-picker>
                    </template>
                  </el-table-column>
                </el-table-column>
                <el-table-column label="考察项目" header-align="center">
                  <template slot-scope="scope">
                    <span style="color: red" class="point" @click="addStandard(index,scope.$index)" v-if="scope.row.standards == null ? true : scope.row.standards.length == 0">请先配置项目<br></span>
                    <span style="color: #0071b4" v-else>
                      <span v-for="(s,sindex) in scope.row.standards" :key="sindex">
                        <span style="color: #D4B249" v-html="s.nationInspectionStandardNum + ':'"></span>
                        &nbsp;&nbsp;&nbsp;<span style="color: red" class="point" @click="subStandard(index, scope.$index, sindex)">删除此标准</span><br>
                        <span class="point" @click="showProjectItem(index, scope.$index, sindex)">{{ s.itemNames }}</span><br>
                      </span>
                      <span class="point" style="color: #67C23A" @click="addStandard(index,scope.$index)">增加标准</span>
                      <span class="point" style="color: #67C23A" @click="cItem(scope.row)">复制</span>
                    </span>
                    <span class="point" style="color: #67C23A;" @click="vItem(index,scope.$index)">粘贴</span>
                    <span class="point" style="color: #67C23A;" @click="vItem(index)">粘贴至全部</span>
                    <div v-if="scope.row.standards instanceof Array ? scope.row.standards.length != 0 : false">
                      <span class="point" @click="showSimpleReport(index,scope.$index)">查看报告配置</span>
                      <span class="point" @click="copyReportToAll(index,scope.$index)">将此报告配置应用至当前所有取样点</span>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="是否为增测项" width="80" header-align="center" align="center">
                  <template slot-scope="scope">
                    <div>
                      <el-radio @change="setCheckFrequency(index)" v-model="scope.row.isStability" :label="1">是</el-radio>
                    </div>
                    <div>
                      <el-radio @change="setCheckFrequency(index)" v-model="scope.row.isStability" :label="0">否</el-radio>
                    </div>
                  </template>
                </el-table-column>
              </el-table>
              <span slot="footer" class="dialog-footer">
                <el-button type="primary" class="pan-btn primary-btn" size="mini" @click="showProjectTime(index)">确定</el-button>
              </span>
            </el-dialog>
          </el-form-item>

          <el-form-item label="目前已稳考次数:" v-if="!onlyGet">
            {{ p.now + ' 次' }}
          </el-form-item>

          <el-form-item label="稳考状态:" v-if="!onlyGet">
            {{ p.status == 0 ? '待完善' : p.status == 1 ? '未完成' : '已完成' }}
          </el-form-item>

          <el-form-item label="批准文号:" v-if="!onlyGet">
            <el-input :id="'approvalNum'+index" v-model="p.approvalNum" size="small" />
          </el-form-item>

          <el-form-item label="存放位置:" v-if="!onlyGet">
            <el-input :id="'storeLocation'+index" v-model="p.storeLocation" size="small" />
          </el-form-item>

          <el-form-item label="稳考目的:" v-if="!onlyGet">
            <el-input :id="'stabilityPurpose'+index" @change="setAllStabilityPurpose(p.stabilityPurpose)" v-model="p.stabilityPurpose" size="small" />
          </el-form-item>

          <el-form-item label="备注:">
            <el-input v-model="p.remark" size="small" />
          </el-form-item>

        </el-form>
      </el-collapse-item>
    </el-collapse>
	  <span slot="footer" class="dialog-footer">
	    <el-button @click="close" size="small">取 消</el-button>
	    <el-button type="primary" class="pan-btn primary-btn" size="small" :loading="submiting" @click="submit">提 交</el-button>
  	</span>

    <standard-drawer
      :childDataForm="grandDataForm"
      @close="closeInner('standardDrawerVisible')"
      @getStandard="getStandardObj"
      v-if="standardDrawerVisible" />

    <standard-select
      v-if="standardSelectVisible"
      @close="closeInner('standardSelectVisible')"
      @getStandard="getStandard"
      :childDataForm="grandDataForm" />

    <simple-report
      v-if="simpleReportVisible"
      @close="closeInner('simpleReportVisible')"
      @getSimpleStandards="getSimpleStandards"
      :childDataForm="grandDataForm" />

    <el-dialog
      :visible.sync="projectVisible"
      :append-to-body="true"
      @before-close="closeInner('projectVisible')"
      width="50%">
      系统检测到此计划未绑定方案，已为您自动搜索出关联此货品的稳考方案，请选择一个方案以便系统自动拉取方案中的某些内容填充到此计划中，以节省您的填写时间
      <el-card>
        <el-radio-group v-model="fullProject">
          <el-radio style="display: block; margin-top: 20px;" v-for="(p,pi) in baseProjects" :key="pi" :label="p">{{ p.showName }}</el-radio>
        </el-radio-group>
      </el-card>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeInner('projectVisible')" size="small">取 消</el-button>
        <el-button type="primary" class="pan-btn primary-btn" size="small" @click="projectFullPlan">选 定</el-button>
      </span>
    </el-dialog>
	</el-dialog>
</template>

<script>
  import MdInput from '@/components/MDinput'
  import { getUnitList,getWorkshopList } from '@/api/check'
  import {
    getBaseTimeList,
    saveOrUpdatePlan,
    stabilityOnlyGet,
    stabilityOnlyPlan,
    getStabilityProjectByProductId,
    getStandardAndItems,
    updateStabilityTimeStatus
  } from '@/api/stability'
	import { CODE_OK } from '@/common/js/config'
  import { isBlank,unique } from '@/common/js/utils'
  import { isPositiveNumber,isPositiveIntegerNumber } from '@/common/js/validate'
  import StandardDrawer from '@/views/modules/base/standard-drawer'
  import StandardSelect from '@/views/modules/base/standard-select'
  import Calc from 'calculatorjs'
  import SimpleReport from '@/views/modules/common/simple-report'
	export default {
		data() {
			return {
				visible: false,
        isUpdate: false,
        onlyPlan: false,
        onlyGet: false,
        submiting: false,
        standardDrawerVisible: false,
        standardSelectVisible: false,
        projectVisible: false,
        simpleReportVisible: false,
        standardForm: {
          standard: '',
          allItems: [],
          checkedItems: []
        },
        waitIndex: {},
        copyStandards: [],
        activeNames: [],
        list: {},
        projects: [],
        zcList: {},
        unitList: [],
        timeList: [],
        grandDataForm: '',
        status0: false,
        baseProjects: [],
        fullProject: '',
        workshopList: []
			}
		},
    components: { MdInput,StandardDrawer,StandardSelect,SimpleReport },
		props: ["childDataForm"],
		created() {
			if (!isBlank(this.childDataForm)) {
        this.init()
        this.projects = this.childDataForm.projects
        //已经领取，只需编写计划
        this.onlyPlan = this.childDataForm.onlyPlan
        //已领取和计划，更新
        this.isUpdate = this.projects[0].planId != undefined && !this.onlyPlan ? true : false
        //只领取，不编写计划
        this.onlyGet = this.childDataForm.onlyGet ? this.childDataForm.onlyGet : false
        if (!this.isUpdate) {
          this.list = this.childDataForm.list
          this.zcList = this.childDataForm.zcList
          if (this.onlyGet) {
            this.getInit()
          }else{
            this.planInit()
          }
        }else{
          this.updateInit()
        }
        this.visible = true
        this.$nextTick(() => {
          this.setCss()
        })
			}
		},
		methods: {
      showSimpleReport(i,j) {
        this.waitIndex.i = i
        this.waitIndex.j = j
        this.grandDataForm = JSON.parse(JSON.stringify(this.projects[i].times[j].standards))
        this.simpleReportVisible = true
      },
      copyReportToAll(index, jndex) {
        this.$confirm('将此份报告的配置(排序、是否打印。。等)应用至当前方案的所有取样点，注意：只能应用当前项目，多余的项目则采用成品报告的缺省配置', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let arr = this.projects[index].times[jndex].standards
          let baseItems = []
          for (let i=0,j=arr.length; i<j; i++) {
            baseItems.push(...JSON.parse(JSON.stringify(arr[i].items)))
          }
          for (let i=0,j=this.projects[index].times.length; i<j; i++) {
            let t = this.projects[index].times[i]
            for (let m=0,n=t.standards.length; m<n; m++) {
              let s = t.standards[m]
              for (let x=0,y=s.items.length; x<y; x++) {
                let it = s.items[x]
                for (let bi of baseItems) {
                  if (it.inspectionItemId == bi.inspectionItemId) {
                    it.isNumber = bi.isNumber
                    it.printValue = bi.printValue
                    it.isPrint = bi.isPrint
                    it.printConfig = bi.printConfig
                    it.orderId = bi.orderId
                    this.projects[index].times[i].standards[m].items[x] = it
                    break
                  }
                }
              }
            }
          }
          this.$message.success('应用成功')
        }).catch(() => {})
      },
      getSimpleStandards(standards) {
        this.projects[this.waitIndex.i].times[this.waitIndex.j].standards = standards
        this.$message.info('修改成功，请提交')
      },
      getStandard(standard) {
        this.standardSelectVisible = false
        if (standard.nationInspectionStandardNum == null) {
          this.$message.error("此标准不符合规定，无国家标准，请先联系QC管理员(刘威)添加此标准的国家标准")
          return
        }
        this.standardForm.standard = standard
        this.standardForm.allItems = standard.items
        let sList = this.projects[this.waitIndex.i].times[this.waitIndex.j].standards
        for (let i=0,j=sList.length; i<j; i++) {
          if (standard.insideInspectionStandardId == sList[i]) {
            this.$message.error("不允许有重复标准出现")
            return
          }
        }
        this.projects[this.waitIndex.i].standards.push(JSON.parse(JSON.stringify(standard)))
        this.$set(standard, 'itemNames', '')
        this.projects[this.waitIndex.i].times[this.waitIndex.j].standards.push(standard)
        this.waitIndex.si = sList.length - 1
        this.showStandardDrawer()
      },
      addStandard(i,j) {
        if (this.projects[i].times[j].status == 2) {
          this.$message.error("无法对已经完成的稳考进行操作")
          return
        }
        this.waitIndex = {
          i: i,
          j: j
        }
        if (this.status0) {
          this.grandDataForm = {
            accordingToInside: this.projects[0].productName
          }
        }
        this.standardSelectVisible = true
      },
      subStandard(i,j,si) {
        if (this.projects[i].times[j].status == 2) {
          this.$message.error("无法对已经完成的稳考进行操作")
          return
        }
        this.projects[i].times[j].standards.splice(si,1)
      },
      vItem(i,j) {
        if (this.copyStandards.length == 0) {
          this.$message.error("无粘贴项目")
          return
        }else{
          if (typeof(j) == 'number') {
            if (this.projects[i].times[j].status == 2) {
              this.$message.error("无法改变已经完成的稳考")
              return
            }
            this.projects[i].times[j].standards = JSON.parse(JSON.stringify(this.copyStandards))
          }else{
            for (let m=0,n=this.projects[i].times.length; m<n; m++) {
              if (this.projects[i].times[m].status != 2) {
                this.projects[i].times[m].standards = JSON.parse(JSON.stringify(this.copyStandards))
              }
            }
          }
        }
      },
      cItem(row) {
        this.copyStandards = JSON.parse(JSON.stringify(row.standards))
        this.$message.success("复制成功，快使用粘贴吧")
      },
      showProjectItem(i,j,si) {
        let rowStd = this.projects[i].times[j].standards[si]
        this.waitIndex = {
          i: i,
          j: j,
          si: si
        }
        for (let x=0,y=this.projects[i].standards.length; x<y; x++) {
          if (rowStd.insideInspectionStandardId == this.projects[i].standards[x].insideInspectionStandardId) {
            this.standardForm.standard = this.projects[i].standards[x]
            this.standardForm.allItems = this.projects[i].standards[x].items
            this.standardForm.checkedItems = rowStd.items
            break
          }
        }
        this.showStandardDrawer()
      },
      showStandardDrawer() {
        this.grandDataForm = {
          standard: this.standardForm.standard,
          allItems: this.standardForm.allItems,
          checkedItems: this.standardForm.checkedItems
        }
        this.standardDrawerVisible = true
      },
      closeInner(val) {
        this[val] = false
      },
      getStandardObj(standardObj) {
        let i = this.waitIndex.i
        let j = this.waitIndex.j
        let si = this.waitIndex.si
        if (this.projects[i].times[j].status == 2) {
          this.$message.info('已完成的取样时间点只能修改项目的报告配置，不能改变项目的数量')
          return
        }
        let standardId = this.standardForm.standard.insideInspectionStandardId
        let nationInspectionStandardNum = this.standardForm.standard.nationInspectionStandardNum
        this.projects[i].times[j].standards[si].items = standardObj.checkedItems.map(it => {
          this.$set(it, 'insideInspectionStandardId', standardId)
          this.$set(it, 'nationInspectionStandardNum', nationInspectionStandardNum)
          return it
        })
        this.projects[i].times[j].standards[si].itemNames = this.projects[i].times[j].standards[si].items.map(it => {
          return it.inspectionItemName
        }).join('、')
      },
      showProjectTime(ix) {
        if (isBlank(this.projects[ix].storeDate)) {
          this.$message.error('请先选择存放日期')
          return
        }
        this.projects[ix].timeVisible = !this.projects[ix].timeVisible
      },
      addTime(i,j) {
        let t = JSON.parse(JSON.stringify(this.projects[i].times[j]))
        t.planTimeId = null
        t.status = 1
        t.timeValue = null
        t.timePoint = null
        t.beginTimePoint = null
        t.endTimePoint = null
        t.timeArr = []
        this.projects[i].times.splice(j+1,0,t)
        this.projects[i].total = this.projects[i].times.length
        this.setCheckFrequency(i)
        this.flushSurplus(i)
      },
      subTime(i,j) {
        if (this.isUpdate) {
          this.projects[i].deleteTimeIds.push(this.projects[i].times[j].planTimeId)
        }
        this.projects[i].times.splice(j,1)
        this.projects[i].total =  this.projects[i].times.length
        this.setCheckFrequency(i)
        this.flushSurplus(i)
      },
      init(){
        getUnitList().then(res => {
          if (res.code === CODE_OK) {
            this.unitList = res.list
          }else{
            this.$message.error(res.msg)
          }
        })
        getBaseTimeList().then(res => {
          if (res.code === CODE_OK) {
            this.timeList = res.list
          }else{
            this.$message.error(res.msg)
          }
        })
        getWorkshopList().then(res => {
          if (res.code === CODE_OK) {
            this.workshopList = res.list
          }else{
            this.$message.error(res.msg)
          }
        })
      },
      setCss() {
        for (let i in this.projects) {
          $("#approvalNum"+i).css({"border-color" : "#007EC8"})
          $("#cpStoreEnv"+i).css({"border-color" : "#007EC8"})
          $("#storeLocation"+i).css({"border-color" : "#007EC8"})
          $("#stabilityPurpose"+i).css({"border-color" : "#007EC8"})
          $("#storeDate"+i).css({"border-color" : "#007EC8"})
        }
      },
      getInit() {
        this.activeNames.push(0)
        this.projects = this.projects.map(p => {
          this.$set(p, 'incomingMiniUnit', {
            unitId: p.incomingMiniUnitId,
            unitName: p.incomingMiniUnitName
          })
          this.$set(p, 'incomingBigUnit', {
            unitId: p.incomingBigUnitId,
            unitName: p.incomingBigUnitName
          })
          return p
        })
      },
      projectFullPlan() {
        let project = this.fullProject
        if (isBlank(this.fullProject)) {
          this.$message.success('请选择至少一个方案进行绑定')
          return
        }
        let plan = this.projects[0]
        let timesT = plan.times
        let timesS = project.times
        //以timesT为准，找对应得timesS
        if (timesT[0].timeId != timesS[0].timeId) {
          //单位不匹配
          this.$message.error('选定的方案取样点时间单位无法匹配于此计划的取样点时间单位,请检查')
          return
        }
        let standardIds = []
        for (let i=0,j=timesT.length; i<j; i++) {
          let t1 = timesT[i]
          for (let x=0,y=timesS.length; x<y; x++) {
            if (t1.timeValue == timesS[x].timeValue) {
              t1.standards = timesS[x].standards.map(s => {
                standardIds.push(s.insideInspectionStandardId)
                this.$set(s,'itemNames',s.items.map(it => {
                return it.inspectionItemName
              }).join('、'))
                return s
              })
              break
            }
          }
        }
        if (standardIds.length != 0) {
          standardIds = unique(standardIds)
          const params1 = {
            standardIds: standardIds,
            isBase: 0
          }
          getStandardAndItems(params1).then(res => {
            if (res.code === CODE_OK) {
              plan.standards = res.info.standards
            }else{
              this.$message.error(res.msg)
            }
          })
        }
        plan.projectId = project.projectId
        if (isBlank(plan.storeEnv)) {
          plan.storeEnv = project.storeEnv
        }
        if (isBlank(plan.envT)) {
          plan.envT = project.envT
        }
        if (isBlank(plan.envH)) {
          plan.envH = project.envH
        }
        if (isBlank(plan.pack)) {
          plan.pack = project.pack
        }
        this.projectVisible = false
      },
      getProjectForFull() {
        const params = {
          productId: this.projects[0].productId,
          stabilityModeId: this.projects[0].stabilityModeId,
          page: 1,
          limit: 999
        }
        getStabilityProjectByProductId(params).then(res => {
          if (res.code === CODE_OK) {
            this.baseProjects = res.info.projects.map(p => {
              p.showName = '【' + p.stabilityModeName + '】' + p.projectName + '(' + p.batchQuantity + p.batchUnitName + '/批)' + '(' + p.checkPeriodValue + p.checkPeriodTimeName + ')' + (p.remark == null ? '' : ('(备注:' + p.remark + ')'))
              return p
            })
            if (this.baseProjects.length == 1) {
              this.fullProject = this.baseProjects[0]
              this.projectFullPlan()
              this.$message.success('已为你自动拉取稳考方案并填充到此计划')
            }else if(this.baseProjects.length != 0){
              this.projectVisible = true
            }else{
              this.$message.error('此货品未绑定方案，无法拉取方案内容，某些内容需要自行填写')
            }
          }else{
            this.$message.error('此货品未绑定方案，无法拉取方案内容，某些内容需要自行填写')
          }
        })
      },
      updateInit() {
        this.activeNames.push(0)
        this.projects = this.projects.map(p => {
          this.status0 = p.status == 0
          this.$set(p,'workshop',{
            workshopId: p.workshopId,
            workshopName: p.workshopName
          })
          this.$set(p, 'incomingMiniUnit', {
            unitId: p.incomingMiniUnitId,
            unitName: p.incomingMiniUnitName
          })
          this.$set(p, 'incomingBigUnit', {
            unitId: p.incomingBigUnitId,
            unitName: p.incomingBigUnitName
          })
          this.$set(p, 'timeVisible', false)
          this.$set(p, 'rowTime', {
            timeId: p.times[0].timeId,
            timeName: p.times[0].timeName
          })
          if (isBlank(p.checkPeriodValue)) {
            this.$set(p, 'checkPeriodValue', p.times[p.times.length-1].timeValue)
          }
          if (isBlank(p.checkPeriodTimeName)) {
            this.$set(p, 'checkPeriodTime', JSON.parse(JSON.stringify(p.rowTime)))
            this.$set(p,'checkPeriodTimeId', p.checkPeriodTime.checkPeriodTimeId)
            this.$set(p,'checkPeriodTimeName', p.checkPeriodTime.checkPeriodTimeName)
          }else{
            this.$set(p, 'checkPeriodTime', {
              timeId: p.checkPeriodTimeId,
              timeName: p.checkPeriodTimeName
            })
          }
          if (this.status0) {
            this.$set(p, 'surplus', p.inventory)
            this.$set(p, 'standards', [])
          }else if(!isBlank(p.inventory)){
            let allGetQuantity = 0
            for (let t of p.times) {
              if (t.getQuantity != null && t.status == 1) {
                allGetQuantity = Calc(allGetQuantity + '+' + t.getQuantity)
              }
            }
            this.$set(p, 'surplus', Calc(p.inventory + '-' + allGetQuantity))
          }else{
            p.inventory = 0
            this.$set(p, 'surplus', 0)
            this.$set(p, 'standards', [])
          }
          this.$set(p, 'allGetQuantity', null)

          if (this.status0) {
            //加速正负15天,长期负15天,正0天
            if (p.stabilityModeName == '加速') {
              this.$set(p,'nInterval', 15)
              this.$set(p,'pInterval', 15)
            }else if(p.stabilityModeName == '长期') {
              this.$set(p,'nInterval', 0)
              this.$set(p,'pInterval', 15)
            }
            let nowT = new Date()
            let now = 0
            p.times = p.times.map(t => {
              t.timePoint = this.moment(p.storeDate).add(t.timeValue, this.timeMap(t.timeName)).format('YYYY-MM-DD HH:mm:ss')
              t.beginTimePoint = this.moment(t.timePoint).subtract(p.nInterval, 'd').format('YYYY-MM-DD HH:mm:ss')
              t.endTimePoint = this.moment(t.timePoint).add(p.pInterval, 'd').format('YYYY-MM-DD HH:mm:ss')
              t.getUnitId = p.incomingMiniUnitId
              t.getUnitName = p.incomingMiniUnitName
              this.$set(t, 'timeArr', [t.beginTimePoint, t.endTimePoint])
              this.$set(t, 'standards', [])
              //如果取样时间点为这个月或者之后，则状态为1(未完成)，否则为2(已完成)，其次更新p.now
              if (this.moment(t.timePoint).isSameOrAfter(nowT, 'month')) {
                t.status = 1
                t.unlock = false
              }else{
                now++
                t.status = 2
                t.unlock = true
              }
              return t
            })
            this.setCheckFrequency(0)
            p.now = now
          }else{
            this.$set(p, 'pInterval', null)
            this.$set(p, 'nInterval', null)
            p.times = p.times.map(t => {
              t.getUnitId = p.incomingMiniUnitId
              t.getUnitName = p.incomingMiniUnitName
              t.items = t.bitmes
              this.$set(t, 'timeArr', [t.beginTimePoint,t.endTimePoint])
              if (!isBlank(t.standards)) {
                t.standards = t.standards.map(s => {
                  this.$set(s,'itemNames',s.items.map(it => {
                    return it.inspectionItemName
                  }).join('、'))
                  return s
                })
              }
              t.unlock = t.status == 2
              return t
            })
          }
          return p
        })
        if (this.status0) {
          this.getProjectForFull()
        }
      },
      changeLock(j) {
        if (this.status0) {
          if (this.projects[0].times[j].unlock) {
            //解锁
            this.projects[0].now--
            this.projects[0].times[j].status = 1
            this.projects[0].times[j].unlock = false
          }else{
            //上锁
            this.projects[0].now++
            if (this.projects[0].now == this.projects[0].total) {
              this.projects[0].status = 2
            }
            this.projects[0].times[j].status = 2
            this.projects[0].times[j].unlock = true
          }
        }else{
          //及时改
          if (this.projects[0].times[j].status == 1 && this.projects[0].times.filter(v => {return v.status == 1}).length == 1) {
            this.$message.error('最后一次无法改为已完成,请联系管理员')
            return
          }
          let unlock = this.projects[0].times[j].unlock
          let status = this.projects[0].times[j].status
          let now = this.projects[0].now
          if (unlock) {
            //已完成=>未完成  2》1
            status = 1
            now--
          }else{
            //未完成=>已完成  1》2
            status = 2
            now++
          }
          unlock = !unlock
          const data = {
            planTimeId: this.projects[0].times[j].planTimeId,
            status: status,
            planId: this.projects[0].planId,
            now: now
          }
          updateStabilityTimeStatus(data).then(res => {
            if (res.code === CODE_OK) {
              this.projects[0].times[j].unlock = unlock
              this.projects[0].times[j].status = status
              this.projects[0].now = now
              this.$message.success('修改成功')
            }else{
              this.$message.error(res.msg)
            }
          })
        }
      },
      planInit() {
        let groupGetQuantity = this.list.groupGetQuantity
        let groupGetUnitId = this.list.groupGetUnitId
        let groupGetUnitName = this.list.groupGetUnitName
        //分配入库量标志
        //1，全分(projects.length == 1)
        //2，按方案分(方案的batchQuantity总和为分样量this.list.groupGetQuantity)
        //3，平均值分
        let dispenseFlag = 1
        if (this.projects.length != 1) {
          //前提单位统一
          if (this.projects[0].batchUnitName == this.list.groupGetUnitName) {
            let quantity = 0
            for (let p of this.projects) {
              quantity = Calc(quantity + '+' + p.batchQuantity)
            }
            if (quantity == groupGetQuantity) {
              dispenseFlag = 2
            }else{
              dispenseFlag = 3
              groupGetQuantity = groupGetQuantity / this.projects.length
            }
          }else{
            dispenseFlag = 3
            groupGetQuantity = groupGetQuantity / this.projects.length
          }
        }
        this.projects = this.projects.map(p => {
          this.$set(p,'planName', this.list.productName+'('+this.list.batchNum+')'+'【'+p.stabilityModeName+'】')
          this.$set(p,'checkId', this.list.checkId)
          this.$set(p,'workshopId', this.zcList.workshopId)
          this.$set(p,'workshopName', this.zcList.workshopName)
          this.$set(p,'workshop',{
            workshopId: p.workshopId,
            workshopName: p.workshopName
          })
          this.$set(p,'productionDate', this.zcList.productionDate)
          this.$set(p,'validityDate', this.zcList.validityDate)
          this.$set(p,'productId', this.list.productId)
          this.$set(p,'productGmp', this.list.productGmp)
          this.$set(p,'productName', this.list.productName)
          this.$set(p,'productSpec', this.list.productSpec)
          this.$set(p,'productPackSpec', this.list.productPackSpec)
          this.$set(p,'approvalNum', null)
          this.$set(p,'batchNum', this.list.batchNum)
          if (dispenseFlag == 2) {
            this.$set(p,'incomingMini', p.batchQuantity)
            this.$set(p,'incomingMiniUnitId', groupGetUnitId)
            this.$set(p,'incomingMiniUnitName', groupGetUnitName)
            this.$set(p,'incomingMiniUnit', {
              unitId: p.incomingMiniUnitId,
              unitName: p.incomingMiniUnitName
            })
            this.$set(p,'inventory', p.batchQuantity)
            this.$set(p,'inventoryUnitId', groupGetUnitId)
            this.$set(p,'inventoryUnitName', groupGetUnitName)
            this.$set(p,'surplus', p.batchQuantity)
            this.$set(p,'incomingBig', p.batchQuantity)
            this.$set(p,'incomingBigUnitId', this.onlyPlan ? this.list.incomingBigUnitId : groupGetUnitId)
            this.$set(p,'incomingBigUnitName', this.onlyPlan ? this.list.incomingBigUnitName : groupGetUnitName)
            this.$set(p,'incomingBigUnit', {
              unitId: p.incomingBigUnitId,
              unitName: p.incomingBigUnitName
            })
          }else{
            this.$set(p,'incomingMini', groupGetQuantity)
            this.$set(p,'incomingMiniUnitId', groupGetUnitId)
            this.$set(p,'incomingMiniUnitName', groupGetUnitName)
            this.$set(p,'incomingMiniUnit', {
              unitId: p.incomingMiniUnitId,
              unitName: p.incomingMiniUnitName
            })
            this.$set(p,'inventory', groupGetQuantity)
            this.$set(p,'inventoryUnitId', groupGetUnitId)
            this.$set(p,'inventoryUnitName', groupGetUnitName)
            this.$set(p,'surplus', groupGetQuantity)
            this.$set(p,'incomingBig', groupGetQuantity)
            this.$set(p,'incomingBigUnitId', this.onlyPlan ? this.list.incomingBigUnitId :  groupGetUnitId)
            this.$set(p,'incomingBigUnitName', this.onlyPlan ? this.list.incomingBigUnitName :  groupGetUnitName)
            this.$set(p,'incomingBigUnit', {
              unitId: p.incomingBigUnitId,
              unitName: p.incomingBigUnitName
            })
          }
          this.$set(p,'checkPeriodTime', {
            timeId: p.checkPeriodTimeId,
            timeName: p.checkPeriodTimeName
          })
          this.$set(p,'storeDate', this.onlyPlan ? this.list.storeDate : null)
          this.$set(p,'cpStoreEnv', null)
          this.$set(p,'storeLocation', null)
          this.$set(p,'stabilityPurpose', null)
          this.$set(p,'total', p.times.length)
          this.$set(p,'now', 0)
          this.$set(p,'status', 1)
          this.$set(p,'rowTime',{
            timeId: p.times[0].timeId,
            timeName: p.times[0].timeName
          })
          this.$set(p,'timeVisible', false)
          this.$set(p,'allGetQuantity', null)
          //加速正负15天,长期负15天,正0天
          if (p.stabilityModeName == '加速') {
            this.$set(p,'nInterval', 15)
            this.$set(p,'pInterval', 15)
          }else if(p.stabilityModeName == '长期') {
            this.$set(p,'nInterval', 0)
            this.$set(p,'pInterval', 15)
          }
          p.times = p.times.map(t => {
            this.$set(t,'timePoint', this.onlyPlan ? this.moment(p.storeDate).add(t.timeValue, this.timeMap(t.timeName)).format('YYYY-MM-DD HH:mm:ss') : null)
            this.$set(t,'beginTimePoint', this.onlyPlan ? this.moment(t.timePoint).subtract(p.nInterval, 'd').format('YYYY-MM-DD HH:mm:ss') : null)
            this.$set(t,'endTimePoint', this.onlyPlan ? this.moment(t.timePoint).add(p.pInterval, 'd').format('YYYY-MM-DD HH:mm:ss') : null)
            this.$set(t,'timeArr', this.onlyPlan ? [t.beginTimePoint,t.endTimePoint] : [])
            this.$set(t,'getQuantity', null)
            //系统默认采用小单位为取样量单位
            this.$set(t,'getUnitId', p.incomingMiniUnitId)
            this.$set(t,'getUnitName', p.incomingMiniUnitName)
            this.$set(t,'status', 1)
            this.$set(t,'isStability', 0)
            t.standards = t.standards.map(s => {
              this.$set(s,'itemNames', s.items.map(it => {
                return it.inspectionItemName
              }).join('、'))
              return s
            })
            return t
          })
          return p
        })
      },
			close(val) {
				this.$emit('close')
				this.visible = false
			},
      setAllItems() {
        this.projects = this.projects.map(p => {
          p.times = p.times.map(t => {
            t.items = []
            if (!isBlank(t.standards)) {
              for (let i=0,j=t.standards.length; i<j; i++) {
                for (let x=0,y=t.standards[i].items.length; x<y; x++) {
                  t.items.push(t.standards[i].items[x])
                }
              }
            }
            return t
          })
          return p
        })
      },
			submit() {
        this.submiting = true
        this.setAllItems()
        if (this.formVerify()) {
          if (!this.onlyGet) {
            this.projects = this.projects.map(p => {
              if (p.times.length > 0) {
                p.times = p.times.map(t => {
                  t.getUnitId = p.incomingMiniUnitId
                  t.getUnitName = p.incomingMiniUnitName
                  return t
                })
                this.$set(p,'total',p.times.length)
                this.$set(p,'now',p.times.filter(t => {return t.status == 2 && t.isStability == 0}).length)
                if (p.total == p.now) {
                  this.$set(p,'status',2)
                }
              }
              return p
            })
            if (this.onlyPlan) {
              const data = {
                plans: this.projects,
                oldPlanId: this.list.oldPlanId
              }
              stabilityOnlyPlan(data).then(res => {
                if (res.code === CODE_OK) {
                  this.$message.success('提交成功')
                  this.$emit('close')
                  this.visible = false
                  this.$emit('submit','submit')
                }else{
                  this.$message.error(res.msg)
                }
              })
            }else{
              const data = {
                isUpdate: this.isUpdate,
                plans: this.projects,
                checkGroupGetId: this.isUpdate ? null : this.list.checkGroupGetId
              }
              saveOrUpdatePlan(data).then(res => {
                if (res.code === CODE_OK) {
                  this.$message.success('提交成功')
                  this.$emit('close')
                  this.visible = false
                  this.$emit('submit','submit')
                }else{
                  this.$message.success(res.msg)
                }
              })
            }
          }else{
            const data = {
              plan: this.projects[0],
              checkGroupGetId: this.list.checkGroupGetId
            }
            stabilityOnlyGet(data).then(res => {
              if (res.code === CODE_OK) {
                this.$message.success('提交成功')
                this.$emit('close')
                this.visible = false
                this.$emit('submit','submit')
              }else{
                this.$message.success(res.msg)
              }
            })
          }
        }
        this.submiting = false
      },
      formVerify() {
        //非系统必须，不影响系统流程的字段，未作非空要求
        for (let p of this.projects) {
          if (isBlank(p.planName)) {
            this.$message.error("随便起一个名？")
            return false
          }
          if (isBlank(p.incomingMini)) {
            this.$message.error("入库数量(小单位)不能为空")
            return false
          }
          if (isBlank(p.incomingMiniUnitId)) {
            this.$message.error("入库数量(小单位)单位名不能为空")
            return false
          }
          if (isBlank(p.incomingBig)) {
            this.$message.error("入库数量(大单位)不能为空")
            return false
          }
          if (isBlank(p.incomingBigUnitId)) {
            this.$message.error("入库数量(大单位)单位名不能为空")
            return false
          }
          if (isBlank(p.storeDate)) {
            this.$message.error("存放时间不能为空")
            return false
          }
          if (!this.onlyGet) {
            if (isBlank(p.inventory)) {
              this.$message.error("库存量不能为空")
              return false
            }
            if (isBlank(p.inventoryUnitId)) {
              this.$message.error("库存量单位名不能为空")
              return false
            }
            if (p.times.length == 0) {
              this.$message.error("取样点不能为空")
              return false
            }
            let allGetQuantity = 0
            for (let i=0,j=p.times.length; i<j; i++) {
              let t = p.times[i]
              if (t.status == 1) {
                if (isBlank(t.timeValue)) {
                  this.$message.error("取样点数值不能为空")
                  return false
                }
                if (isBlank(t.timeId)) {
                  this.$message.error("取样点单位不能为空")
                  return false
                }
                if (isBlank(t.getQuantity)) {
                  this.$message.error("取样量数值不能为空")
                  return false
                }
                if (t.getQuantity == 0) {
                  this.$message.error("取样量数值不能为0")
                  return false
                }
                if (t.items.length == 0) {
                  this.$message.error("检验项目不能为空")
                  return false
                }
                allGetQuantity = Calc(allGetQuantity + '+' + t.getQuantity)
                if (isBlank(t.getUnitId)) {
                  this.$message.error("取样量单位不能为空")
                  return false
                }
                if (isBlank(t.timePoint)) {
                  this.$message.error("具体取样时间不能为空")
                  return false
                }
                for (let x=0,y=p.times.length; x<y; x++) {
                  let t2 = p.times[x]
                  if (t2.status == 1) {
                    if (i!=x) {
                      if (!isBlank(t2.timeValue)) {
                        if (t.timeValue == t2.timeValue && t.isStability == 0 && t2.isStability == 0) {
                          this.$message.error("取样点数值不能有重复")
                          return false
                        }
                      }
                      if (!isBlank(t2.timePoint)) {
                        if (t.timePoint == t2.timePoint && t.isStability == 0 && t2.isStability == 0) {
                          this.$message.error("具体取样时间不能重复")
                          return false
                        }
                      }
                    }
                  }
                }
              }
            }
            if (allGetQuantity > p.incomingMini) {
              this.$message.error("总取样量不能大于总入库量")
              return false
            }
          }
        }
        return true
      },
      setTimePoint(i) {
        if (this.onlyGet) return
        let nInterval = this.projects[i].nInterval
        let pInterval = this.projects[i].pInterval
        this.projects[i].times = this.projects[i].times.map(t => {
          t.timePoint = this.moment(this.projects[i].storeDate).add(t.timeValue, this.timeMap(t.timeName)).format('YYYY-MM-DD HH:mm:ss')
          return this.flushTimeArr2(t,nInterval,pInterval)
        })
      },
      timeMap(key) {
        const map = {
          '分': 'm',
          '时': 'h',
          '日': 'd',
          '周': 'w',
          '月': 'M',
          '年': 'y'
        }
        return map[key]
      },
      setWorkshop(i) {
        this.projects[i].workshopId = this.projects[i].workshop.workshopId
        this.projects[i].workshopName = this.projects[i].workshop.workshopName
      },
      setIncomingMini(i) {
        if (!isPositiveNumber(this.projects[i].incomingMini)) {
          this.$message.error('请输入正数值类型')
          this.projects[i].incomingMini = null
        }
        this.projects[i].inventory = this.projects[i].incomingMini
        this.projects[i].surplus = this.projects[i].inventory
      },
      setIncomingMiniUnit(i) {
        this.$message.warning("注意，系统默认此单位为实际取样量单位")
        this.projects[i].incomingMiniUnitId = this.projects[i].incomingMiniUnit.unitId
        this.projects[i].incomingMiniUnitName = this.projects[i].incomingMiniUnit.unitName
        this.projects[i].inventoryUnitId = this.projects[i].incomingMiniUnit.unitId
        this.projects[i].inventoryUnitName = this.projects[i].incomingMiniUnit.unitName
        if (!this.onlyGet) {
          this.projects[i].times = this.projects[i].times.map(t => {
            t.getUnitId = this.projects[i].incomingMiniUnit.unitId
            t.getUnitName = this.projects[i].incomingMiniUnit.unitName
            return t
          })
        }
      },
      setIncomingBig(i) {
        if (!isPositiveNumber(this.projects[i].incomingBig)) {
          this.$message.error('请输入正数值类型')
          this.projects[i].incomingBig = null
        }
      },
      setIncomingBigUnit(i) {
        this.projects[i].incomingBigUnitId = this.projects[i].incomingBigUnit.unitId
        this.projects[i].incomingBigUnitName = this.projects[i].incomingBigUnit.unitName
      },
      setCheckPeriodValue(i) {
        if (!isPositiveNumber(this.projects[i].checkPeriodValue)) {
          this.$message.error('请输入正数值类型')
          this.projects[i].checkPeriodValue = null
        }
      },
      setCheckPeriodTime(i) {
        this.projects[i].checkPeriodTimeId = this.projects[i].checkPeriodTime.timeId
        this.projects[i].checkPeriodTimeName = this.projects[i].checkPeriodTime.timeName
      },
      setRowTimeValue(i,j) {
        if (!isPositiveNumber(this.projects[i].times[j].timeValue)) {
          this.$message.error('请输入正数值类型')
          this.projects[i].times[j].timeValue = null
        }else{
          let t = this.projects[i].times[j]
          if (!isPositiveIntegerNumber(t.timeValue)) {
            this.$message({
              message: '取样点带小数，系统无法计算，请自行修改具体取样时间和取样时间段',
              type: 'error',
              duration: 5500
            })
            t.timePoint = null
            t.beginTimePoint = null
            t.endTimePoint = null
            t.timeArr = []
          }else{
            t.timePoint = this.moment(this.projects[i].storeDate).add(t.timeValue, this.timeMap(t.timeName)).format('YYYY-MM-DD HH:mm:ss')
            t = this.flushTimeArr2(t,this.projects[i].nInterval,this.projects[i].pInterval)
          }
          this.projects[i].times[j] = t
          this.setCheckFrequency(i)
        }
      },
      setRowTime(ix) {
        for (let i=0,j=this.projects[ix].times.length; i<j; i++) {
          this.projects[ix].times[i].timeId = this.projects[ix].rowTime.timeId
          this.projects[ix].times[i].timeName = this.projects[ix].rowTime.timeName
        }
        this.setCheckFrequency(i)
      },
      setCheckFrequency(i) {
        if (this.projects[i].times.length == 0) return
        let stabilityTime = this.projects[i].times.filter(t => {
          return t.isStability == 0
        })
        let timeValueStr = stabilityTime.map(t => {
          return t.timeValue
        }).join('、')
        let val = '于第' + timeValueStr + this.projects[i].rowTime.timeName + this.projects[i].rowTime.timeName + '末取样检测'
        this.$set(this.projects[i],'checkFrequency',val)
        this.$set(this.projects[i],'total',stabilityTime.length)
      },
      setRowGetQuantity(i,j) {
        if (!isPositiveNumber(this.projects[i].times[j].getQuantity)) {
          this.$message.error('请输入正数值类型')
          this.projects[i].times[j].getQuantity = null
        }
        this.flushSurplus(i)
      },
      flushSurplus(i) {
        let allGetQuantity = 0
        for (let t of this.projects[i].times) {
          if (t.getQuantity != null && t.status == 1) {
            allGetQuantity = Calc(allGetQuantity + '+' + t.getQuantity)
          }
        }
        this.projects[i].surplus = Calc(this.projects[i].inventory + '-' + allGetQuantity)
      },
      setAllGetQuantity(i) {
        if (!isPositiveNumber(this.projects[i].allGetQuantity)) {
          this.$message.error('请输入正数值类型')
          this.projects[i].allGetQuantity = null
          return
        }
        let q = JSON.parse(JSON.stringify(this.projects[i].allGetQuantity))
        let unfinishCount = 0
        this.projects[i].times = this.projects[i].times.map(t => {
          if (t.status == 1) {
            unfinishCount++
            t.getQuantity = q
          }
          return t
        })
        let val = this.projects[i].inventory + '-(' + this.projects[i].allGetQuantity + '*' + unfinishCount + ')'
        this.projects[i].surplus = Calc(val)
      },
      setNInterval(i) {
        if (!isPositiveIntegerNumber(this.projects[i].nInterval)) {
          this.$message.error('请输入正整数')
          this.projects[i].nInterval = null
          return
        }
        this.flushTimeArr(i)
      },
      flushTimeArr(i,j) {
        let nInterval = this.projects[i].nInterval
        let pInterval = this.projects[i].pInterval
        if (j) {
          //刷新某一个
          this.projects[i].times[j] = this.flushTimeArr2(this.projects[i].times[j],nInterval,pInterval)
        }else{
          //刷新全部
          this.projects[i].times = this.projects[i].times.map(t => {return this.flushTimeArr2(t,nInterval,pInterval)})
        }
      },
      flushTimeArr2(t,n,p) {
        if (t.status != 2) {
          t.beginTimePoint = this.moment(t.timePoint).subtract(n, 'd').format('YYYY-MM-DD HH:mm:ss')
          t.endTimePoint = this.moment(t.timePoint).add(p, 'd').format('YYYY-MM-DD HH:mm:ss')
          t.timeArr = []
          t.timeArr.push(t.beginTimePoint)
          t.timeArr.push(t.endTimePoint)
        }
        return t
      },
      setPInterval(i) {
        if (!isPositiveIntegerNumber(this.projects[i].pInterval)) {
          this.$message.error('请输入正整数')
          this.projects[i].pInterval = null
          return
        }
        this.flushTimeArr(i)
      },
      setAllStabilityPurpose(val) {
        if (!isBlank(val)) {
          for (let i=0,j=this.projects.length; i<j; i++) {
            this.projects[i].stabilityPurpose = val
          }
        }
      }
    }
  }
</script>

<style scoped>
  >>> .el-form-item__content .el-input-group {
    vertical-align: middle !important;
  }
  >>> .el-form-item__content {
    line-height: 40px !important;
  }
  >>> .el-form-item {
    margin-bottom: 0px !important;
  }
</style>
